﻿package com.vicentediaz.view.imageviewer
{
    import com.greensock.*;
    import com.greensock.events.*;
    import com.myp.flow.*;
    import com.myp.mystage.*;
    import com.vicentediaz.data.*;
    import com.vicentediaz.flow.*;
    import com.vicentediaz.view.general.*;
    import com.vicentediaz.view.menu.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.media.*;
    import flash.ui.*;

    public class CanvasGallery extends Sprite
    {
        private var container:Sprite;
        private var channel:SoundChannel;
        private const MINIATURE_DIST:uint = 30;
        private var currentMousePos:Point;
        private var blackBorder:Sprite;
        private var area:Sprite;
        private var sound:Sound;
        private var selectedId:uint;
        private var volume:Number = 0;
        private var tip:ClickDragTip;
        private var prevPoint:Point;
        private const SCALE_MULT:Number = 0.01;
        private var trans:SoundTransform;
        private var imageFlow:Flow;
        public var drawing:Boolean;
        private var lastMousePos:Point;
        private var moving:Boolean;
        private var cursor:Cursor;
        private var newVolume:Number = 0;
        private var nextPoint:Point;
        private var zDepthImage:ZDepthImage;
        private var currentId:uint;
        private static var instance:CanvasGallery;

        public function CanvasGallery()
        {
            this.prevPoint = new Point();
            this.nextPoint = new Point();
            this.area = new Sprite();
            this.container = new Sprite();
            this.blackBorder = new Sprite();
            var _loc_1:Number;
            this.blackBorder.scaleY = 1.3;
            this.blackBorder.scaleX = _loc_1;
            this.currentId = 0;
            addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage, false, 0, true);
            this.moving = false;
            this.lastMousePos = new Point();
            this.currentMousePos = new Point();
            this.sound = Sounds.getInstance().getById("WIND");
            this.channel = this.sound.play();
            this.trans = this.channel.soundTransform;
            this.trans.volume = 0;
            this.channel.soundTransform = this.trans;
            this.channel.addEventListener(Event.SOUND_COMPLETE, this.onSoundComplete, false, 0, true);
            this.imageFlow = Flow.level(ImageFlow.LEVEL);
            this.imageFlow.addEventListener(FlowEvent.REQUEST, this.onFlowRequest, false, 0, true);
            this.cursor = new Cursor();
            this.tip = new ClickDragTip();
            this.zDepthImage = ZDepthImage.getInstance();
            this.tip.cacheAsBitmap = true;
            this.cursor.cacheAsBitmap = true;
            instance = this;
            return;
        }// end function

        private function addPointAt(param1:Point, param2:Number = 1) : void
        {
            var _loc_3:* = new CanvasGalleryItem(this.currentId % Gallery.getInstance().loadedThumbs);
            _loc_3.x = param1.x;
            _loc_3.y = param1.y;
            var _loc_4:* = param2 > 1 ? (1) : (param2);
            _loc_3.scaleY = param2 > 1 ? (1) : (param2);
            _loc_3.scaleX = _loc_4;
            TweenMax.from(_loc_3, 0.6, {scaleX:0, scaleY:0});
            _loc_3.addEventListener(MouseEvent.CLICK, this.onItemClick, false, 0, true);
            var _loc_4:String;
            _loc_4.currentId = this.currentId++;
            this.container.addChild(_loc_3);
            return;
        }// end function

        private function drawMiniatureLine(param1:Point, param2:Point) : void
        {
            var _loc_3:* = Point.distance(param1, param2);
            var _loc_4:* = int(_loc_3 / this.MINIATURE_DIST);
            var _loc_5:uint;
            while (_loc_5++ < _loc_4)
            {
                // label
                this.addPointAt(Point.interpolate(param1, param2, _loc_5 / _loc_4), _loc_3 * this.SCALE_MULT + Math.random() * 0.2);
            }// end while
            stage.addEventListener(MouseEvent.MOUSE_DOWN, this.onCanvasMouseDown, false, 0, true);
            return;
        }// end function

        private function onCanvasMouseDown(param1:MouseEvent) : void
        {
            if (!contains(param1.target as DisplayObject))
            {
                return;
            }// end if
            if (param1.target is CanvasGalleryItem)
            {
                return;
            }// end if
            this.zDepthImage.orbitate = true;
            stage.addEventListener(MouseEvent.MOUSE_UP, this.onCanvasMouseUp, false, 0, true);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, this.onCanvasMouseMove, false, 0, true);
            this.prevPoint.x = mouseX;
            this.prevPoint.y = mouseY;
            TweenMax.to(this.tip, 0.3, {alpha:0, visible:false});
            TweenMax.to(this.blackBorder, 0.2, {scaleX:1, scaleY:1, delay:0.1});
            this.drawing = true;
            return;
        }// end function

        private function onCanvasMouseUp(param1:MouseEvent) : void
        {
            stage.removeEventListener(MouseEvent.MOUSE_UP, this.onCanvasMouseUp);
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.onCanvasMouseMove);
            TweenMax.killTweensOf(this.blackBorder);
            TweenMax.to(this.blackBorder, 0.3, {scaleX:1.3, scaleY:1.3});
            this.zDepthImage.orbitate = false;
            this.drawing = false;
            return;
        }// end function

        private function onAddedToStage(param1:Event) : void
        {
            stage.addEventListener(MouseEvent.MOUSE_DOWN, this.onCanvasMouseDown, false, 0, true);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, this.onStageMouseDown, false, 0, true);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, this.onStageMouseMoveCursor, false, 0, true);
            MyStage.getInstance().stage.addEventListener(Event.RESIZE, this.onResize, false, 0, true);
            addChild(this.blackBorder);
            addChild(this.area);
            addChild(this.container);
            stage.addChild(this.cursor);
            stage.addChild(this.tip);
            this.onResize(null);
            return;
        }// end function

        private function showNextImage(param1:TweenEvent) : void
        {
            if (Flow.level(MainFlow.LEVEL).currentSection != MainFlow.IMAGEN)
            {
                Flow.level(MainFlow.LEVEL).request(MainFlow.IMAGEN, "CanvasGallery");
            }// end if
            Flow.level(ImageFlow.LEVEL).request(this.selectedId.toString(), "CanvasGallery");
            return;
        }// end function

        private function onStageMouseMoveCursor(param1:MouseEvent) : void
        {
            this.cursor.visible = contains(param1.target as DisplayObject);
            this.cursor.x = mouseX - 8;
            this.cursor.y = mouseY - 8;
            this.tip.x = mouseX;
            this.tip.y = mouseY - 10;
            this.tip.visible = this.cursor.visible;
            if (this.cursor.visible)
            {
                Mouse.hide();
            }
            else
            {
                Mouse.show();
            }// end else if
            return;
        }// end function

        private function onItemClick(param1:MouseEvent) : void
        {
            this.selectedId = CanvasGalleryItem(param1.target).id;
            Sounds.getInstance().playById("CLICK_IMAGE", 1, (-(stage.stageWidth / 2 - stage.mouseX)) / stage.stageWidth);
            this.hideAllThumbs();
            return;
        }// end function

        private function onAllThumbsHide(param1:TweenEvent) : void
        {
            while (this.container.numChildren)
            {
                // label
                this.container.removeChildAt(0);
            }// end while
            alpha = 1;
            this.container.cacheAsBitmap = false;
            return;
        }// end function

        private function onFlowRequest(param1:FlowEvent) : void
        {
            this.hideAllThumbs(false);
            return;
        }// end function

        private function onCanvasMouseMove(param1:MouseEvent) : void
        {
            if (!ToolButtons.enabledBrush)
            {
                return;
            }// end if
            this.nextPoint.x = mouseX;
            this.nextPoint.y = mouseY;
            if (Point.distance(this.prevPoint, this.nextPoint) > this.MINIATURE_DIST)
            {
                this.drawMiniatureLine(this.prevPoint, this.nextPoint);
                this.prevPoint.x = this.nextPoint.x;
                this.prevPoint.y = this.nextPoint.y;
            }// end if
            return;
        }// end function

        private function onStageMouseDown(param1:MouseEvent) : void
        {
            if (!contains(param1.target as DisplayObject))
            {
                return;
            }// end if
            this.moving = true;
            this.lastMousePos.x = mouseX;
            this.lastMousePos.y = mouseY;
            stage.addEventListener(MouseEvent.MOUSE_UP, this.onStageMouseUp, false, 0, true);
            addEventListener(Event.ENTER_FRAME, this.onTickVolume, false, 0, true);
            return;
        }// end function

        private function onSoundComplete(param1:Event) : void
        {
            var _loc_2:* = this.trans.volume;
            this.channel = this.sound.play();
            this.trans = this.channel.soundTransform;
            this.trans.volume = _loc_2;
            this.channel.soundTransform = this.trans;
            this.channel.addEventListener(Event.SOUND_COMPLETE, this.onSoundComplete, false, 0, true);
            return;
        }// end function

        private function hideAllThumbs(param1:Boolean = true) : void
        {
            var _loc_4:uint;
            var _loc_2:* = new TimelineMax();
            var _loc_3:uint;
            while (_loc_3++ < this.container.numChildren)
            {
                // label
                CanvasGalleryItem(this.container.getChildAt(_loc_3)).stopMouseEvents();
            }// end while
            if (numChildren > 200)
            {
                this.container.cacheAsBitmap = true;
                _loc_2.insert(TweenMax.to(this.container, 0.8, {alpha:0}), 0);
            }
            else
            {
                _loc_4 = 0;
                while (_loc_4++ < this.container.numChildren)
                {
                    // label
                    _loc_2.insert(TweenMax.to(this.container.getChildAt(_loc_4), 0.3, {alpha:0, visible:false}), Math.random() * 0.5);
                }// end while
            }// end else if
            _loc_2.addEventListener(TweenEvent.COMPLETE, this.onAllThumbsHide, false, 0, true);
            if (param1)
            {
                _loc_2.addEventListener(TweenEvent.COMPLETE, this.showNextImage, false, 0, true);
            }// end if
            return;
        }// end function

        private function onStageMouseUp(param1:MouseEvent) : void
        {
            this.moving = false;
            stage.removeEventListener(MouseEvent.MOUSE_UP, this.onStageMouseUp);
            return;
        }// end function

        private function onTickVolume(param1:Event) : void
        {
            var e:* = param1;
            if (this.moving)
            {
                this.currentMousePos.x = mouseX;
                this.currentMousePos.y = mouseY;
                this.newVolume = Point.distance(this.lastMousePos, this.currentMousePos) * 0.1;
                this.lastMousePos.x = mouseX;
                this.lastMousePos.y = mouseY;
            }
            else
            {
                this.newVolume = 0;
            }// end else if
            this.volume = this.volume - (this.volume - this.newVolume) * 0.05;
            this.volume = this.volume < 0 ? (0) : (this.volume > 1 ? (1) : (this.volume));
            if (this.volume == 0 && !this.moving)
            {
                removeEventListener(Event.ENTER_FRAME, this.onTickVolume);
            }// end if
            try
            {
                this.trans = this.channel.soundTransform;
                this.trans.volume = this.volume;
                this.trans.pan = (-(stage.stageWidth / 2 - mouseX)) / stage.stageWidth;
                this.channel.soundTransform = this.trans;
            }// end try
            catch (e:Error)
            {
            }// end catch
            return;
        }// end function

        private function onResize(param1:Event) : void
        {
            var _loc_2:* = MyStage.getInstance();
            this.area.graphics.clear();
            this.area.graphics.beginFill(255, 0);
            this.area.graphics.drawRect(0, 0, _loc_2.width, _loc_2.height);
            this.area.graphics.endFill();
            this.blackBorder.graphics.clear();
            this.blackBorder.graphics.beginFill(0, 1);
            this.blackBorder.graphics.drawRect(-_loc_2.centerX, -_loc_2.centerY, _loc_2.width, _loc_2.height);
            this.blackBorder.graphics.drawRect(-_loc_2.centerX + 50, -_loc_2.centerY + 40, _loc_2.width - 100, _loc_2.height - 130);
            this.blackBorder.graphics.endFill();
            this.blackBorder.x = _loc_2.centerX;
            this.blackBorder.y = _loc_2.centerY;
            return;
        }// end function

        public static function getInstance() : CanvasGallery
        {
            return instance;
        }// end function

    }
}
